package br.edu.ufcg.lsd.wbis.control.user;

import br.edu.ufcg.lsd.wbis.control.exception.BusinessValidationException;
import br.edu.ufcg.lsd.wbis.control.exception.ErrorType;
import br.edu.ufcg.lsd.wbis.control.util.AbstractValidator;

/**
 * Validates the user fields according to the business logic rules to be
 * followed.
 * 
 * @author Renato Miceli
 */
public class UserValidator extends AbstractValidator {

	/**
	 * Validates all the user fields. Each field is considered to be valid if it
	 * is neither <code>null</code> nor blank.
	 * 
	 * @param login
	 *            the user's login
	 * @param password
	 *            the user's password
	 * @param userName
	 *            the user's name
	 * @param email
	 *            the user's email
	 * @param level
	 *            the user's level
	 * @throws BusinessValidationException
	 *             in case any of the fields is not valid
	 */
	public static void validateUser(String login, String password, String userName, String email, UserLevel level) throws BusinessValidationException {
		validateUserLogin(login);
		validateUserEmail(email);
		validateUserName(userName);
		validateUserPassword(password);
		validateUserLevel(level);
	}

	/**
	 * Validates the user name. A name is valid if it is neither
	 * <code>null</code> nor blank.
	 * 
	 * @param userName
	 *            the name to be validated.
	 * @throws BusinessValidationException
	 *             in case the name is not valid
	 */
	public static void validateUserName(String userName) throws BusinessValidationException {
		validateNotNullNorBlank(userName);
	}

	/**
	 * Validates the user email. An email is valid if it is neither
	 * <code>null</code> nor blank.
	 * 
	 * @param email
	 *            the email to be validated.
	 * @throws BusinessValidationException
	 *             in case the email is not valid
	 */
	public static void validateUserEmail(String email) throws BusinessValidationException {
		validateNotNullNorBlank(email);
	}

	/**
	 * Validates the user password. A password is valid if it is neither
	 * <code>null</code> nor blank.
	 * 
	 * @param password
	 *            the password to be validated.
	 * @throws BusinessValidationException
	 *             in case the password is not valid
	 */
	public static void validateUserPassword(String password) throws BusinessValidationException {
		validateNotNullNorBlank(password);
	}

	/**
	 * Validates the user level. A user level is valid if it is not
	 * <code>null</code>.
	 * 
	 * @param level
	 *            the user level to be validated.
	 * @throws BusinessValidationException
	 *             in case the level is not valid
	 */
	public static void validateUserLevel(UserLevel level) throws BusinessValidationException {
		if (level == null) {
			throw new BusinessValidationException(ErrorType.NULL_VALUE);
		}
	}

	/**
	 * Validates the user login. A login is valid if it is neither
	 * <code>null</code> nor blank.
	 * 
	 * @param login
	 *            the login to be validated.
	 * @throws BusinessValidationException
	 *             in case the login is not valid
	 */
	public static void validateUserLogin(String login) throws BusinessValidationException {
		validateNotNullNorBlank(login);
	}

}
